import { Controller } from "../../mvc/core/Controller";
import { Dispatcher } from "../../mvc/core/Dispatcher";
import { EventName } from "../mvc/EventName";
import { DataEvent } from "../events/DataEvent";
import { AutoFightRule } from "../rules/AutoFightRule";
import { MsgManager } from "../manager/MsgManager";
import { Language } from "../../../extend/language/Language";
import { RolePlayer } from "../scene/player/entity/RolePlayer";
import { ThingUtil } from "../scene/layer/utils/ThingUtil";
import { BossRule } from "../rules/BossRule";
import { MonsterPlayer } from "../scene/player/entity/MonsterPlayer";
import { AIManager } from "../scene/ai/AIManager";
import { AIType } from "../scene/ai/AIType";
import { Game } from "../Game";
//class AutoFightController
    
    export  class AutoFightController extends Controller
    {
       

        protected /* override */ initServer(): void
        {
            Dispatcher.addEventListener(EventName.BeginToFight, this.onBeginToFight);
            Dispatcher.addEventListener(EventName.BeginToFightShortcut, this.onBeginToFightShortcut);
            Dispatcher.addEventListener(EventName.OverFight, this.onOverFight);
            Dispatcher.addEventListener(EventName.SetAutoFightSuccess, this.onSetAutoFight);
            Dispatcher.addEventListener(EventName.AutoFightGuideBegin, this.onAutoFightGuideBegin);
            Dispatcher.addEventListener(EventName.AutoFightPointShortcut, this.onAutoFightPointShortcutHandler);
            return;
        }

        /* internal  */onBeginToFight(arg1: DataEvent): void
        {
            AutoFightRule.flag = AutoFightRule.AutoFightPanel;
            this.beginToFight();
            return;
        }

        /* internal  */onBeginToFightShortcut(arg1: DataEvent): void
        {
            AutoFightRule.flag = AutoFightRule.AutoFightShoutcut;
            this.beginToFight();
            return;
        }

        /* internal  */onAutoFightPointShortcutHandler(arg1: DataEvent): void
        {
            if (cache.copy.isHeroBattleCopy()) 
            {
                MsgManager.showRollTipsMsg(Language.getString(200222));
                return;
            }
            if (!AutoFightRule.isAutoFight()) 
            {
                if (RolePlayer.instance.swimming) 
                {
                    return;
                }
                Dispatcher.dispatchEvent(new DataEvent(EventName.AutoFightKeyToFight));
            }
            RolePlayer.instance.fightAtPointStart();
            return;
        }

        /* internal  */onAutoFightGuideBegin(arg1: DataEvent): void
        {
            AutoFightRule.flag = AutoFightRule.AutoFightHide;
            this.beginToFight();
            return;
        }

        /* internal  */beginToFight(): void
        {
            var loc1=null;
            if (ThingUtil.entityUtil.isAutoFightAble(ThingUtil.selectEntity) && RolePlayer.instance.isCanAttackByPtP(ThingUtil.selectEntity)) 
            {
                if (BossRule.isCollectBoss((ThingUtil.selectEntity as MonsterPlayer).bossType)) 
                {
                    AIManager.onAIControl(ThingUtil.selectEntity, AIType.AI_AutoFight);
                }
                else if (ThingUtil.selectEntity.entityInfo.isCanAttack()) 
                {
                    AIManager.onAIControl(ThingUtil.selectEntity, AIType.AI_AutoFight);
                }
            }
            else 
            {
                loc1 = cache.scene.getAutoFightBoss(ThingUtil.entityUtil.getMonsterCodeList());
                AIManager.onAIControl(loc1, AIType.AI_AutoFight);
            }
            return;
        }

        /* internal  */onOverFight(arg1: DataEvent): void
        {
            AIManager.stopCheckBossTimer();
            return;
        }

        /* internal  */onSetAutoFight(arg1: DataEvent): void
        {
            var loc1=null;
            AutoFightRule.flag = AutoFightRule.AutoFightPanel;
            if (Game.isSceneInit()) 
            {
                if (AutoFightRule.isAutoFight()) 
                {
                    if (ThingUtil.entityUtil.isAutoFightAble(ThingUtil.selectEntity) && RolePlayer.instance.isCanAttackByPtP(ThingUtil.selectEntity)) 
                    {
                        if (BossRule.isCollectBoss((ThingUtil.selectEntity as MonsterPlayer).bossType)) 
                        {
                            AIManager.onAIControl(ThingUtil.selectEntity, AIType.AI_AutoFight);
                        }
                        else if (ThingUtil.selectEntity.entityInfo.isCanAttack()) 
                        {
                            AIManager.onAIControl(ThingUtil.selectEntity, AIType.AI_AutoFight);
                        }
                    }
                    else 
                    {
                        loc1 = cache.scene.getAutoFightBoss(ThingUtil.entityUtil.getMonsterCodeList());
                        AIManager.onAIControl(loc1, AIType.AI_AutoFight);
                    }
                }
            }
            return;
        }
    }
